package hoang.android.app.json;

import hoang.android.app.info.WebServiceConfig;
import hoang.android.app.model.TripInfo;
import hoang.android.app.model.TripInfoList;
import hoang.android.app.model.Video;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ParserJson {
	
	/**
	 * parser location list
	 * 
	 * @param object
	 * @return
	 * @throws JSONException
	 */

	public static ArrayList<TripInfo> parserLocationList(JSONObject object) throws JSONException{
		
		ArrayList<TripInfo> arrayList = new ArrayList<TripInfo>();
		
		/**
		 * "info":[
			    {
			        "loc_id":"107",
			        "loc_name":"Alton",
			        "toll_road":1",
			        "road_name":"241/261/133"
			    },
		 */
		
		JSONArray arrayTrip = object.getJSONArray(WebServiceConfig.PARAM_INFOR);
		for (int i = 0; i < arrayTrip.length(); i++) {
			JSONObject objectTrip = arrayTrip.getJSONObject(i);
			TripInfo infor = new TripInfo();
			
			infor.setLoc_id(objectTrip.getString(WebServiceConfig.PARAM_LOC_ID));
			infor.setLoc_name(objectTrip.getString(WebServiceConfig.PARAM_LOC_NAME));
			infor.setRoad_name(objectTrip.getString(WebServiceConfig.PARAM_ROAD_NAME));
			infor.setToll_road(objectTrip.getString(WebServiceConfig.PARAM_TOLL_ROAD));
			
			arrayList.add(infor);
		}
		
		return arrayList;
	}
	
	
	public static ArrayList<TripInfo> parserListTrip(JSONObject object) throws JSONException{
		
		ArrayList<TripInfo> arrayList = new ArrayList<TripInfo>();
		/**
		 * start_date=2013-06-21&
		 * from_loc_id=4&action=addtrip&
		 * to_loc_id=3 &
		 * tokenID=/WG1paVPJp4C3yhZE2w2fYzVxXUfuixiNoHPVO2Y&
		 * timestamp=1321700334
		 */
		
		JSONArray arrayTrip = object.getJSONArray(WebServiceConfig.PARAM_INFOR);
		for (int i = 0; i < arrayTrip.length(); i++) {
			JSONObject objectTrip = arrayTrip.getJSONObject(i);
			TripInfo infor = new TripInfo();
			
			infor.setLoc_id(objectTrip.getString(WebServiceConfig.PARAM_LOC_ID));
			infor.setLoc_name(objectTrip.getString(WebServiceConfig.PARAM_LOC_NAME));
			infor.setRoad_name(objectTrip.getString(WebServiceConfig.PARAM_ROAD_NAME));
			infor.setToll_road(objectTrip.getString(WebServiceConfig.PARAM_TOLL_ROAD));
			
			arrayList.add(infor);
		}
		
		return arrayList;
	}
	
	/**
	 * {"success":1,"status":200,"message":"","amount_due":9.75","previous_amount_due":1.00","info":
	 * [{"trip_num":"1","from_loc_id":"4","to_loc_id":3","trip_date":"07/01/2013","trip_amount":2.25"},
	 * {"trip_num":"2","from_loc_id":"110","to_loc_id":106","trip_date":"07/01/2013":"trip_amount":6.50"}]}
	 */
	
	public static TripInfoList getTripInfoList(JSONObject json) throws Exception{
		TripInfoList tripInfoList = new TripInfoList();
		
		String amount_due = json.getString("amount_due");
		String previous_amount_due = json.getString("previous_amount_due");
		
		
		
		return tripInfoList;
	}
	
	//------------YOUTUBE----------------//
		public static ArrayList<Video> parserGetListVideo(JSONObject jsonObject) throws JSONException{
			ArrayList<Video> arrayVideos = new ArrayList<Video>();
			
			JSONObject objectFeed = jsonObject.getJSONObject("feed");
			JSONArray arrayEntry = objectFeed.getJSONArray("entry");
			for (int i = 0; i < arrayEntry.length(); i++) {
				
				Video video = new Video();
				
				JSONObject objectEntry = arrayEntry.getJSONObject(i);
				JSONObject objectMediaGroup = objectEntry.getJSONObject("media$group");
				JSONObject objectMediaTitle = objectMediaGroup.getJSONObject("media$title");
				
				//lay title
				video.setTitleVideo(objectMediaTitle.getString("$t"));
				
				//lay duration
				JSONObject objectMediaDuration = objectMediaGroup.getJSONObject("yt$duration");
				video.setDurationVideo(objectMediaDuration.getString("seconds"));
				
				//lay so nguoi xem video nay
				JSONObject objectViews = objectEntry.getJSONObject("yt$statistics");
				video.setNumberViewsVideo(objectViews.getString("viewCount")+" views");
				
				//lay link cua thumbnail
				JSONArray arrayThumnail = objectMediaGroup.getJSONArray("media$thumbnail");
				JSONObject objectThumbnail = arrayThumnail.getJSONObject(0);
				video.setUrlThumbnail(objectThumbnail.getString("url"));
				
				//lay id video
				JSONObject objectVideoID = objectMediaGroup.getJSONObject("yt$videoid");
				video.setIdVideo(objectVideoID.getString("$t"));
				
				arrayVideos.add(video);
				
			}
			
			return arrayVideos;
		}
}
